package com.zgt.web.action;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.zgt.dao.TeacherDao;
import com.zgt.model.Research;
import com.zgt.model.Teacher;
import com.zgt.service.ResearchService;
import com.zgt.service.TeacherService;
import com.zgt.util.ExcelUtil;
import com.zgt.util.MD5;
import com.zgt.util.Page;
import com.zgt.util.ToExcel;
import com.zgt.web.AbstractAction;

@Controller("teacherAction")
@Scope("prototype")
public class TeacherAction extends AbstractAction implements
		ModelDriven<Teacher> {

	private String key;

	private String keyType;
	private MD5 m = new MD5();
	private Page page = new Page();
	private Map request = (Map) ActionContext.getContext().get("request");
	private int research_id;
	private ResearchService researchService;

	private Map session = ActionContext.getContext().getSession();
	private String submitFlag;
	private Teacher teacher = new Teacher();

	private TeacherService teacherService;

	private File uploadExcle;
	private String uploadExcleContentType;
	private String uploadExcleFileName;

	public String add() {
		if (submitFlag == null) {
			return INPUT;
		}
		teacherService.addTeacher(teacher);
		request.put("url", "teacher_list.action");
		return SUCCESS;
	}

	public String batch() {
		if (submitFlag == null) {
			return INPUT;
		}
		List temList;
		String realpath = (ServletActionContext.getServletContext()
				.getRealPath("/upload"));
		File file = new File(realpath);
		File newFile = new File(file, uploadExcleFileName);
		if (!file.exists())
			file.mkdirs();
		try {
			FileUtils.copyFile(uploadExcle, newFile);

			temList = ToExcel.readExcel(newFile);

			teacherService.addTeachers(temList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.put("url", "teacher_list.action");
		return SUCCESS;
	}

	public String delete() {
		teacherService.deleteTeacherById(teacher.getId());
		request.put("url", "teacher_list.action");
		return SUCCESS;
	}

	public String edit() {
		if (submitFlag == null) {
			teacher = teacherService.getTeacherById(teacher.getId());
			request.put("teacher", teacher);
			return INPUT;
		}
		if (!teacher.getPassword().trim().isEmpty()
				&& teacher.getPassword() != null) {
			teacher.setPassword(m.getMD5ofStr(teacher.getPassword()));
		} else {
			Teacher temp = teacherService.getTeacherById(teacher.getId());
			teacher.setPassword(temp.getPassword());
		}
		teacherService.updateTeacher(teacher);
		request.put("url", "teacher_list.action");
		return SUCCESS;
	}

	public String modify() {
		if (submitFlag == null) {
			teacher = (Teacher) session.get("user");
			teacher = teacherService.getTeacherById(teacher.getId());
			request.put("teacher", teacher);
			return INPUT;
		}
		if (!teacher.getPassword().trim().isEmpty()
				&& teacher.getPassword() != null) {
			teacher.setPassword(m.getMD5ofStr(teacher.getPassword()));
		} else {
			Teacher temp = teacherService.getTeacherById(teacher.getId());
			teacher.setPassword(temp.getPassword());
		}
		teacherService.updateTeacher(teacher);
		request.put("url", "teacher_modify.action");
		return SUCCESS;
	}

	public String student() {
		teacher = (Teacher) session.get("user");
		teacher = teacherService.getTeacherById(teacher.getId());
		request.put("students", teacher.getStudents());
		
		return INPUT;
	}

	@Override
	public String execute() throws Exception {
		return super.execute();
	}

	public String getKey() {
		return key;
	}

	public String getKeyType() {
		return keyType;
	}

	public Teacher getModel() {
		return teacher;
	}

	public Page getPage() {
		return page;
	}

	public int getResearch_id() {
		return research_id;
	}

	public ResearchService getResearchService() {
		return researchService;
	}

	public String getSubmitFlag() {
		return submitFlag;
	}

	public File getUploadExcle() {
		return uploadExcle;
	}

	public String getUploadExcleContentType() {
		return uploadExcleContentType;
	}

	public String getUploadExcleFileName() {
		return uploadExcleFileName;
	}

	public String info() {
		teacher = teacherService.getTeacherById(teacher.getId());
		request.put("teacher", teacher);
		return INPUT;

	}

	public String list() {
		request.put("page", teacherService.listTeacher(page));
		return INPUT;
	}

	public String login() {
		teacher.setPassword(m.getMD5ofStr(teacher.getPassword()));
		teacher = teacherService.checkLogin(teacher);
		if (teacher != null) {
			session.put("user", teacher);
			session.put("scope", "teacher");
			return "login";
		}
		return ERROR;
	}

	public String logout() {
		session.clear();
		return ERROR;
	}

	public String search() {
		teacherService.listSearch(page, key, keyType);
		request.put("key", key);
		request.put("keyType", keyType);
		return INPUT;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public void setKeyType(String keyType) {
		this.keyType = keyType;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public void setResearch_id(int research_id) {
		this.research_id = research_id;
	}

	@Resource
	public void setResearchService(ResearchService researchService) {
		this.researchService = researchService;
	}

	public void setSubmitFlag(String submitFlag) {
		this.submitFlag = submitFlag;
	}

	@Resource
	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public void setUploadExcle(File uploadExcle) {
		this.uploadExcle = uploadExcle;
	}

	public void setUploadExcleContentType(String uploadExcleContentType) {
		this.uploadExcleContentType = uploadExcleContentType;
	}

	public void setUploadExcleFileName(String uploadExcleFileName) {
		this.uploadExcleFileName = uploadExcleFileName;
	}

	public void toExcle() {
		List<Teacher> teachers = teacherService.toExcle(key, keyType);

		HttpServletResponse response = ServletActionContext.getResponse();
		HSSFWorkbook wb = new HSSFWorkbook();

		HSSFSheet sheet = wb.createSheet("Sheet1");
		String[] tableHeader = new String[teachers.size()];
		Method[] methods = Teacher.class.getDeclaredMethods();
		for (Method me : methods) {
			ExcelUtil.setCellValue(sheet, 0, 1, "Id");
			ExcelUtil.setCellValue(sheet, 1, 1, "工号");
			ExcelUtil.setCellValue(sheet, 2, 1, "姓名");
			ExcelUtil.setCellValue(sheet, 3, 1, "性别");
			ExcelUtil.setCellValue(sheet, 4, 1, "职称");
			ExcelUtil.setCellValue(sheet, 5, 1, "学历");
			ExcelUtil.setCellValue(sheet, 6, 1, "学位");
			ExcelUtil.setCellValue(sheet, 7, 1, "学生人数");
		}
		int t = 0;
		for (int i = 2; i < teachers.size() + 2; i++) {
			Teacher teacher = (Teacher) teachers.get(t);
			ExcelUtil.setCellValue(sheet, 0, i, teacher.getId() + "");
			ExcelUtil.setCellValue(sheet, 1, i, teacher.getWorkNum());
			ExcelUtil.setCellValue(sheet, 2, i, teacher.getName());
			ExcelUtil.setCellValue(sheet, 3, i, teacher.getSex());
			ExcelUtil.setCellValue(sheet, 4, i, teacher.getTitle());
			ExcelUtil.setCellValue(sheet, 5, i, teacher.getEdu());
			ExcelUtil.setCellValue(sheet, 6, i, teacher.getDegree());
			ExcelUtil.setCellValue(sheet, 7, i, teacher.getStudentCount() + "");
			t++;

		}
		ExcelUtil.createTitle(sheet, tableHeader);
		if (key != null) {
			try {
				ExcelUtil.print2003Excel(wb, response, null, null);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void validate() {
		List<Research> researchs = getResearchService().listAllResearch();
		request.put("researchs", researchs);

		if (submitFlag == null) {
			return;
		}
		if ("upload".equals(submitFlag)) {
			return;
		}
		if (teacher != null) {
			checkEmpty(teacher.getWorkNum(), "登录名不能为空！");
		}

	}

}
